import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login.vue'
import Dashboard from '@/views/Dash.vue'
import VipVue from '@/views/user/Vip.vue'
import CrmVue from '@/views/user/Crm.vue'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { title: 'Login' }
    },
    {
      path: '/',
      name: 'Dashboard',
      component: Dashboard,
      meta: {
        title: 'Dashboard',
        requiresAuth: true
      },
      children: [
        {
          path: '/vip',
          name: 'Vip',
          component: VipVue,
          meta: {
            title: 'Vip',
            requiresAuth: true
          }
        },
        {
          path: '/crm',
          name: 'Crm',
          component: CrmVue,
          meta: {
            title: 'Crm',
            requiresAuth: true
          }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!localStorage.getItem('token')) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
